package mate.task.odin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

/**
 * The activity that displays the view for creating a new item
 * @author Chris
 *
 */
public class ToDoItemCreateActivity extends Activity{
	
	UserDatabase db;
	UserAccount user;
	ToDoList list;
	EditText name, description, tags, location, duedate;
	
	/**
	 * The Create activity for Android
	 * Builds the initial interface
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		// Removes the top title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.todolistitemcreate);
		
		db = UserDatabase.getDatabaseInstance();
		user = db.getCurrentUser();
		list = user.getList();
		
		name = (EditText) findViewById(R.id.shortDescription);
		description = (EditText) findViewById(R.id.longDescription);
		tags = (EditText) findViewById(R.id.tags);
		location = (EditText) findViewById(R.id.location);
		duedate = (EditText) findViewById(R.id.duedate);
		
		
		
		//Enter submits
        name.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        description.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        tags.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        location.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        duedate.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
		
		
		
		Button cancel = (Button) findViewById(R.id.cancelItem);
		cancel.setOnClickListener(new View.OnClickListener() {
		
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent listView = new Intent("mate.task.odin.TODOLISTACTIVITY");
				startActivity(listView);	
			}
		});
		Button create = (Button) findViewById(R.id.confirmItem);
		create.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				submit();
			}
		});
	}
	
	public void submit(){
		try{
			list.addItem(name.getText().toString(), description.getText().toString(), tags.getText().toString(), location.getText().toString(), duedate.getText().toString());
			Intent listView = new Intent("mate.task.odin.TODOLISTACTIVITY");
			startActivity(listView);
		}catch(Exception e){
			//print error
			AlertDialog.Builder alert = new AlertDialog.Builder(ToDoItemCreateActivity.this);
			alert.setMessage("Input date as MM/DD/YYYY");
			alert.setTitle("Invalid Date Format");
			alert.setCancelable(true);
			alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {						
				@Override
				public void onClick(DialogInterface dialog, int which) {							
				}
			});
			alert.show();
		}
	}
	
	/**
	 * Pause for Android
	 * Does not finish the activity
	 */
	public void onPause() {
		super.onPause();
		finish();
	}
	

	
}